# Makefile for XLinix #

# Xlinix kernel 的入口
# 必须与 load.inc 中的 KernelEntryPointPhyAddr 一致
ENTRYPOINT	= 0x30400

# kernel 入口在 kernel 文件的偏移
ENTRYOFFSET	= 0x400

# 编译参数
ASM			= nasm
DASM		= ndisasm
CC			= gcc
LD			= ld
ASMBFLAGS	= -I Boot/Include
ASMKFLAGS	= -I Include -f elf32 
CFLAGS		= -I Include -c -fno-builtin -m32 -fno-stack-protector
LDFLAGS		= -m elf_i386 -Ttext $(ENTRYPOINT)
DASMFLAGS	= -u -o $(ENTRYPOINT) -e $(ENTRYOFFSET)

# 文件
XLINIXBOOT	= Boot/boot.bin Boot/loader.bin
XLINIXKERNEL= Kernel/kernel.bin
OBJS		= Kernel/kernel.o Kernel/start.o Kernel/i8259.o Kernel/global.o Kernel/protect.o\
			Kernel/main.o Kernel/clock.o Kernel/syscall.o Kernel/proc.o Kernel/keyboard.o\
			Kernel/tty.o Kernel/console.o Kernel/printf.o Kernel/vsprintf.o Lib/klib.o Lib/klibc.o Lib/string.o
DASMOUTPUT	= kernel.bin.asm

# 全部伪目标
.PHONY : everything final image clean realclean disasm all buildimg

# 默认执行位置
everything : $(XLINIXBOOT) $(XLINIXKERNEL)

all : realclean everything

final : all clean

image : final buildimg

clean :
	rm -f $(OBJS)

realclean :
	rm -f $(OBJS) $(XLINIXBOOT) $(XLINIXKERNEL)

disasm :
	$(DASM) $(DASMFLAGS) $(XLINIXKERNEL) > $(DASMOUTPUT)

# 假设 xlinix.img 文件在当前文件夹
# 将 "boot.bin" 和 "loader.bin" 存入 xlinix.img
buildimg :
	sudo mount xlinix.img ./Floppy -o loop
	sudo cp -f Boot/loader.bin ./Floppy
	sudo cp -f Kernel/kernel.bin ./Floppy
	sudo umount  ./Floppy

Boot/boot.bin : Boot/boot.asm Boot/Include/load.inc Boot/Include/fat12hdr.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<

Boot/loader.bin : Boot/loader.asm Boot/Include/load.inc Boot/Include/fat12hdr.inc Boot/Include/pm.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<

$(XLINIXKERNEL) : $(OBJS)
	$(LD) $(LDFLAGS) -o $(XLINIXKERNEL) $(OBJS)

Kernel/kernel.o : Kernel/kernel.asm Include/sconst.inc
	$(ASM) $(ASMKFLAGS) -o $@ $<

Kernel/syscall.o : Kernel/syscall.asm Include/sconst.inc
	$(ASM) $(ASMKFLAGS) -o $@ $<

Kernel/start.o : Kernel/start.c ./Include/type.h ./Include/const.h ./Include/protect.h ./Include/proto.h ./Include/string.h ./Include/global.h ./Include/proc.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/mian.o : Kernel/main.c ./Include/type.h ./Include/const.h ./Include/protect.h ./Include/proto.h ./Include/string.h ./Include/global.h ./Include/proc.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/i8259.o : Kernel/i8259.c ./Include/type.h ./Include/const.h ./Include/protect.h ./Include/proto.h ./Include/string.h ./Include/global.h ./Include/proc.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/global.o: Kernel/global.c Include/type.h Include/const.h Include/protect.h Include/proto.h Include/global.h Include/proc.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/protect.o: Kernel/protect.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/clock.o: Kernel/clock.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/proc.o: Kernel/proc.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/keyboard.o: Kernel/keyboard.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/keymap.h Include/keyboard.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/tty.o: Kernel/tty.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/keyboard.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/console.o: Kernel/console.c Include/type.h Include/const.h Include/protect.h Include/global.h Include/proto.h Include/proc.h Include/string.h Include/keyboard.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Kernel/printf.o: Kernel/printf.c Include/type.h Include/const.h 
	$(CC) $(CFLAGS) -o $@ $<

Kernel/vsprintf.o: Kernel/vsprintf.c Include/type.h Include/const.h Include/string.h
	$(CC) $(CFLAGS) -o $@ $<

Lib/klibc.o : Lib/klib.c Include/type.h Include/const.h Include/protect.h Include/proto.h Include/string.h Include/global.h Include/proc.h Include/console.h Include/tty.h
	$(CC) $(CFLAGS) -o $@ $<

Lib/klib.o : Lib/klib.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<

Lib/string.o : Lib/string.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<